package com.h3bpm.web.util;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.lang3.ObjectUtils;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author gaoqiang
 * @date 2022/1/26
 */
public class DBUtil {
    //这里可以设置数据库名称
    private final static String URL = "jdbc:sqlserver://10.66.38.245;DatabaseName=tjecdata";
    private static final String USER = "BPM";
    private static final String PASSWORD = "Tjec@20220124";
    private static Connection conn = null;
    private static Statement stmt = null;
    private static BasicDataSource dataSource = new BasicDataSource();
    //静态代码块（将加载驱动、连接数据库放入静态块中）
    static {
        dataSource.setUrl(URL);
        dataSource.setPassword(PASSWORD);
        dataSource.setUsername(USER);
        dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        dataSource.setInitialSize(20);
        dataSource.setMaxActive(50);
        dataSource.setMinIdle(5);
        dataSource.setMaxIdle(10);
    }
    public static DataSource getDataSource() {
        return dataSource;
    }
    //对外提供一个方法来获取数据库连接
    public static Connection getConnection() {
        try{
            conn = dataSource.getConnection();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return conn;
    }

}
